home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Report Writers / Crystal Repot 9.0 Full CD version / Setup.exe / SRC / HOARDDLL.ZIP / 3rdParty / hoard / libhoard-2.0.2 / superblock.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-06-18  |  7.8 KB  |  317 lines

  1. ///-*-C++-*-//////////////////////////////////////////////////////////////////
  2. //
  3. // Hoard: A Fast, Scalable, and Memory-Efficient Allocator
  4. //        for Shared-Memory Multiprocessors
  5. // Contact author: Emery Berger, http://www.cs.utexas.edu/users/emery
  6. //
  7. // Copyright (c) 1998-2000, The University of Texas at Austin.
  8. //
  9. // This library is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU Library General Public License as
  11. // published by the Free Software Foundation, http://www.fsf.org.
  12. //
  13. // This library is distributed in the hope that it will be useful, but
  14. // WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. // Library General Public License for more details.
  17. //
  18. //////////////////////////////////////////////////////////////////////////////
  19.  
  20.  
  21. //////////////////////////////////////////////////////////////////////////////
  22. //
  23. // Note: This file was modified by Crystal Decisions in June 2002.
  24. //
  25. //////////////////////////////////////////////////////////////////////////////
  26.  
  27.  
  28. /*
  29.   superblock.h
  30.   ------------------------------------------------------------------------
  31.   The superblock class controls a number of blocks (which are
  32.   allocatable units of memory).
  33.   ------------------------------------------------------------------------
  34.   @(#) $Id: superblock.h,v 1.37 2000/03/21 18:19:55 emery Exp $
  35.   ------------------------------------------------------------------------
  36.   Emery Berger                    | <http://www.cs.utexas.edu/users/emery>
  37.   Department of Computer Sciences |             <http://www.cs.utexas.edu>
  38.   University of Texas at Austin   |                <http://www.utexas.edu>
  39.   ========================================================================
  40. */
  41.  
  42. #ifndef _SUPERBLOCK_H_
  43. #define _SUPERBLOCK_H_
  44.  
  45. #include "config.h"
  46.  
  47. #include <assert.h>
  48. #include <new.h>
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51.  
  52. #include "arch-specific.h"
  53. #include "block.h"
  54.  
  55. class hoardHeap; // forward declaration
  56. class processHeap; // forward declaration
  57.  
  58. class superblock {
  59.  
  60. public:
  61.  
  62.   // Construct a superblock for a given size class and set the heap
  63.   // owner.
  64.   superblock (int numblocks,
  65.           int sizeclass,
  66.           hoardHeap * owner);
  67.  
  68.   ~superblock (void)
  69.     {}
  70.  
  71.   // Make (allocate or re-use) a superblock for a given size class.
  72.   static superblock * makeSuperblock (int sizeclass, processHeap * pHeap);
  73.  
  74.   // Find out who allocated this superblock.
  75.   inline hoardHeap * getOwner (void);
  76.  
  77.   // Set the superblock's owner.
  78.   inline void setOwner (hoardHeap * o);
  79.  
  80.   // Get a block from the superblock.
  81.   inline block * getBlock (void);
  82.  
  83.   // Put a block back in the superblock.
  84.   inline void putBlock (block * b);
  85.  
  86.   // How many blocks are available?
  87.   inline int getNumAvailable (void);
  88.  
  89.   // How many blocks are there, in total?
  90.   inline int getNumBlocks (void);
  91.  
  92.   // What size class are blocks in this superblock?
  93.   inline int getBlockSizeClass (void);
  94.  
  95. #ifdef CRYSTAL_HOARD
  96.   // What is the class of this superblock?
  97.   inline int getSuperblockClass (void);
  98. #endif
  99.  
  100.   // Insert this superblock before the next one.
  101.   inline void insertBefore (superblock * nextSb);
  102.  
  103.   // Return the next pointer (to the next superblock in the list).
  104.   inline superblock * const getNext (void);
  105.  
  106.   // Return the prev pointer (to the previous superblock in the list).
  107.   inline superblock * const getPrev (void);
  108.  
  109.   // Compute the 'fullness' of this superblock.
  110.   inline void computeFullness (void);
  111.  
  112.   // Return the 'fullness' of this superblock.
  113.   inline int getFullness (void);
  114.   
  115. #if HEAP_FRAG_STATS
  116.   // Return the amount of waste in every allocated block.
  117.   int getMaxInternalFragmentation (void);
  118. #endif
  119.  
  120.   // Remove this superblock from its linked list.
  121.   inline void remove (void);
  122.  
  123.   // Is this superblock valid? (i.e.,
  124.   // does it have the right magic number?)
  125.   inline int isValid (void);
  126.  
  127.   void upLock (void) {
  128.     hoardLock (_upLock);
  129.   }
  130.  
  131.   void upUnlock (void) {
  132.     hoardUnlock (_upLock);
  133.   }
  134.  
  135. private:
  136.  
  137.   // Disable copying and assignment.
  138.  
  139.   superblock (const superblock&);
  140.   const superblock& operator= (const superblock&);
  141.  
  142.   // Used for sanity checking.
  143.   enum { SUPERBLOCK_MAGIC = 0xCAFEBABE };
  144.  
  145. #if HEAP_DEBUG
  146.   unsigned long _magic;
  147. #endif
  148.  
  149.   const int     _sizeClass;    // The size class of blocks in the superblock.
  150.   const int     _numBlocks;    // The number of blocks in the superblock.
  151.   int        _numAvailable;    // The number of blocks available.
  152.   int        _fullness;        // How full is this superblock?
  153.                           // (which SUPERBLOCK_FULLNESS group is it in)
  154.   block *    _freeList;      // A pointer to the first free block.
  155.   hoardHeap *     _owner;        // The heap who owns this superblock.
  156.   superblock *     _next;        // The next superblock in the list.
  157.   superblock *     _prev;        // The previous superblock in the list.
  158.  
  159.   hoardLockType _upLock;    // Lock this when moving a superblock to the global (process) heap.
  160.  
  161. #ifdef CRYSTAL_HOARD
  162.   int  _superblockClass;  // The class of this superblock
  163. #endif
  164.  
  165.   // We insert a cache pad here to prevent false sharing with the
  166.   // first block (which immediately follows the superblock).
  167.  
  168.   double _pad[CACHE_LINE / sizeof(double)];
  169. };
  170.  
  171.  
  172. hoardHeap * superblock::getOwner (void)
  173. {
  174.   assert (isValid());
  175.   hoardHeap * o = _owner;
  176.   return o;
  177. }
  178.  
  179.  
  180. void superblock::setOwner (hoardHeap * o) 
  181. {
  182.   assert (isValid());
  183.   _owner = o;
  184. }
  185.  
  186.  
  187. block * superblock::getBlock (void)
  188. {
  189.   assert (isValid());
  190.   // Pop off a block from this superblock's freelist,
  191.   // if there is one available.
  192.   if (_freeList == NULL) {
  193.     // The freelist is empty.
  194.     assert (getNumAvailable() == 0);
  195.     return NULL;
  196.   }
  197.   assert (getNumAvailable() > 0);
  198.   block * b = _freeList;
  199.   _freeList = _freeList->getNext();
  200.   _numAvailable--;
  201.  
  202.   b->setNext(NULL);
  203.  
  204.   computeFullness();
  205.  
  206.   return b;
  207. }
  208.  
  209.  
  210. void superblock::putBlock (block * b)
  211. {
  212.   assert (isValid());
  213.   // Push a block onto the superblock's freelist.
  214.   assert (b->isValid());
  215.   assert (b->getSuperblock() == this);
  216.   assert (getNumAvailable() < getNumBlocks());
  217.   b->setNext (_freeList);
  218.   _freeList = b;
  219.   _numAvailable++;
  220.   computeFullness();
  221. }
  222.  
  223. int superblock::getNumAvailable (void)
  224. {
  225.   assert (isValid());
  226.   return _numAvailable;
  227. }
  228.  
  229.  
  230. int superblock::getNumBlocks (void)
  231. {
  232.   assert (isValid());
  233.   return _numBlocks;
  234. }
  235.  
  236.  
  237. int superblock::getBlockSizeClass (void)
  238. {
  239.   assert (isValid());
  240.   return _sizeClass;
  241. }
  242.  
  243.  
  244. #ifdef CRYSTAL_HOARD
  245. int superblock::getSuperblockClass (void)
  246. {
  247.   assert (isValid());
  248.   return _superblockClass;
  249. }
  250. #endif
  251.  
  252.  
  253. superblock * const superblock::getNext (void)
  254. {
  255.   assert (isValid());
  256.   return _next; 
  257. }
  258.  
  259. superblock * const superblock::getPrev (void)
  260. {
  261.   assert (isValid());
  262.   return _prev; 
  263. }
  264.  
  265.  
  266. void superblock::insertBefore (superblock * nextSb) {
  267.   assert (isValid());
  268.   // Insert this superblock before the next one (nextSb).
  269.   assert (nextSb != this);
  270.   _next = nextSb;
  271.   if (nextSb) {
  272.     _prev = nextSb->_prev;
  273.     nextSb->_prev = this;
  274.   }
  275. }
  276.  
  277.  
  278. void superblock::remove (void) {
  279.   // Remove this superblock from a doubly-linked list.
  280.   if (_next) {
  281.     _next->_prev = _prev;
  282.   }
  283.   if (_prev) {
  284.     _prev->_next = _next;
  285.   }
  286.   _prev = NULL;
  287.   _next = NULL;
  288. }
  289.  
  290.  
  291. int superblock::isValid (void)
  292. {
  293.   assert (_numBlocks > 0);
  294.   assert (_numAvailable <= _numBlocks);
  295.   assert (_sizeClass >= 0);
  296. #ifdef CRYSTAL_HOARD
  297.   assert (_superblockClass >= 0);
  298. #endif
  299.   return 1;
  300. }
  301.  
  302.  
  303. void superblock::computeFullness (void)
  304. {
  305.   assert (isValid());
  306.   _fullness = (((SUPERBLOCK_FULLNESS_GROUP - 1)
  307.         * (getNumBlocks() - getNumAvailable())) / getNumBlocks());
  308. }
  309.  
  310. int superblock::getFullness (void)
  311. {
  312.   assert (isValid());
  313.   return _fullness;
  314. }
  315.  
  316. #endif // _SUPERBLOCK_H_
  317.